<template>
  <div class="gender">
    <van-picker
      title="选择性别"
      show-toolbar
      :columns="columns"
      @confirm="onConfirm"
      @cancel="onCancel"
      :default-index="value"
    />
  </div>
</template>

<script>
import { updateUserInfo } from '@/api/user'
export default {
  name: 'Gender',
  components: {},
  props: {
    value: {
      type: Number, // 0男,1女
      required: true
    }
  },
  data() {
    return {
      columns: ['男', '女']
    }
  },
  created() {},
  mounted() {},
  activited() {},
  update() {},
  beforeRouteUpdate() {},
  methods: {
    // 确定更改性别
    onConfirm(value, index) {
      this.updateUserInfo(index)
    },

    // 取消,关闭弹窗
    onCancel() {
      this.$emit('close')
    },

    // 改变性别
    async updateUserInfo(index) {
      try {
        await updateUserInfo({ gender: index })
        // console.log(data)
        // 改变视图
        this.$emit('input', index)
        // 关闭弹窗
        this.$emit('close')
        // 轻提示
        this.$toast.success('修改成功')
      } catch (error) {
        this.$toast.fail('编辑失败')
      }
    }
  },
  filter: {},
  computed: {},
  watch: {}
}
</script>

<style lang="less" scoped></style>
